package net.jlibrary.curbparking.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.curbparking.action.vo.ParkRecordVo;
import net.jlibrary.curbparking.service.IParkRecordManager;

@RestController
@RequestMapping("/parkrecord")
public class ParkRecordAction {

	@Autowired
	private IParkRecordManager recordManager;

	@RequestMapping(value = "/parkRecordAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/curbparking/parkRecordAdminView");
	}

	// 获得停车记录列表
	@RequestMapping(value = "/recordAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(ParkRecordVo recordVo, PaginationParam param) {
		Pagination pagination = recordManager.listsPage(recordVo, param);
		return pagination;
	}

	// 停车记录详情
	@RequestMapping(value = "/recordAdminJsonDetail", method = { RequestMethod.GET })
	public ParkRecordVo detail(ParkRecordVo recordVo) {
		recordVo = recordManager.get(recordVo);
		return recordVo;
	}

	// 初始化停车记录
	@RequestMapping(value = "/recordAdminJsonAddFormInit", method = { RequestMethod.GET })
	public ParkRecordVo addFormInit() {
		ParkRecordVo recordVo = new ParkRecordVo();
		return recordVo;
	}

	// 保存停车记录信息
	@RequestMapping(value = "/recordAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") ParkRecordVo recordVo, @Token String token, BindingResult result) {
		recordManager.save(recordVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 修改停车记录信息
	@RequestMapping(value = "/recordAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") ParkRecordVo recordVo, @Token String token, BindingResult result) {
		recordManager.update(recordVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 删除停车记录信息
	@RequestMapping(value = "/recordAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		ParkRecordVo recordVo = new ParkRecordVo();
		recordVo.setId(id);
		recordManager.delete(recordVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 恢复停车记录信息
	@RequestMapping(value = "/recordAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		ParkRecordVo recordVo = new ParkRecordVo();
		recordVo.setId(id);
		recordManager.renew(recordVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}
}
